﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.Security;

public partial class SiteWorx_UserControls_WebPartManager : System.Web.UI.UserControl
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
            Visible = !Utility.PageInPreviewMode();
        AddWebPartHandlers();
    }

    private void AddWebPartHandlers()
    {
        SiteWorx_UserControls_PageEditorToolbar webPartToolbar = 
            (SiteWorx_UserControls_PageEditorToolbar)lvwSiteWorxMenu.FindControl("ctlPageEditorToolbar");

        if (webPartToolbar != null)
        {
            webPartToolbar.AddWebPart += 
                new SiteWorx_UserControls_PageEditorToolbar.AddWebPartEventHandler(webPartToolbar_AddWebPart);
            
            foreach (GenericWebPart genericWebPart in wpmDefault.WebParts)
                ((BaseWebPart)genericWebPart.Controls[0]).DeleteWebPart += 
                    new BaseWebPart.DeleteWebPartEventHandler(SiteWorx_UserControls_WebPartManager_DeleteWebPart);

        }

    }

    private void webPartToolbar_AddWebPart(UserControl sender, EventArgs e)
    {
        AddWebPartEventArgs eventArgs = (AddWebPartEventArgs)e;
        GenericWebPart webPart;
        WebPartZone webPartZone;
        UserControl userControl;

        userControl = (UserControl)LoadControl(eventArgs.ControlFilePath);
        userControl.ID = eventArgs.ControlId;
        userControl.Attributes["Title"] = eventArgs.ControlTitle;

        webPart = wpmDefault.CreateWebPart(userControl);
        webPartZone = (WebPartZone)wpmDefault.Zones[0];
        wpmDefault.AddWebPart(webPart, webPartZone, 0);
    }

    private void SiteWorx_UserControls_WebPartManager_DeleteWebPart(UserControl sender, EventArgs e)
    {
        wpmDefault.DeleteWebPart(wpmDefault.WebParts[sender.ID]);
    }

    private void ChangeToCatalogMode()
    {
        wpmDefault.DisplayMode = WebPartManager.CatalogDisplayMode;
        foreach (WebPartZone zone in wpmDefault.Zones)
            zone.PartChromeType = PartChromeType.Default;

    }

    private void ChangeToBrowseMode()
    {
        wpmDefault.DisplayMode = WebPartManager.BrowseDisplayMode;
        foreach (WebPartZone zone in wpmDefault.Zones)
            zone.PartChromeType = PartChromeType.None;
    }

    protected void lnkbtnLogout_Click(object sender, EventArgs e)
    {
        ChangeToBrowseMode();
        FormsAuthentication.SignOut();
        Asah.SiteWorx.Search.UserSearch.ClearSearchableContent();
        Session.Abandon();
        Response.Redirect("~/Login.aspx", true);
    }

    protected void lnkbtnDeletePage_Click(object sender, EventArgs e)
    {
        BasePage page = (BasePage)Page;
        if (page.CanDeletePage)
        {
            Asah.SiteWorx.SiteMap.PageInfo pageInfo = Asah.SiteWorx.Site.GetSite().CurrentPage;

            Asah.SiteWorx.SiteMap.Page.DeletePage(pageInfo.ID,pageInfo.Url);
            Response.Redirect("Default.aspx", true);
        }
    }

    protected void wpmDefault_PreRender(object sender, EventArgs e)
    {
        if (!Page.IsPostBack & Page.User.IsInRole("Administrator"))
            if (wpmDefault.Zones.Count > 0) // we have webpart zones defined and can change to catalog mode.
                ChangeToCatalogMode();
    }
}
